package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.ExExamPapperMapper;
import com.macro.mall.mapper.ExExamResultMapper;
import com.macro.mall.mapper.ExQuestionOptionMapper;
import com.macro.mall.mapper.ExQuestionsMapper;
import com.macro.mall.model.*;
import com.macro.mall.service.ExExamPapperService;
import com.macro.mall.service.TiTrainingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ExExamPapperServiceImpl implements ExExamPapperService {
    @Autowired
    private ExExamPapperMapper examPapperMapper;

    @Autowired
    private ExQuestionsMapper questionMapper;

    @Autowired
    private ExQuestionOptionMapper questionOptionMapper;

    @Autowired
    private  TiTrainingService trainingService;

    @Autowired
    private ExExamResultMapper exExamResultMapper;

    @Override
    public ExExamPapper getById(Long id) throws Exception{
        ExExamPapper exExamPapper = examPapperMapper.selectByPrimaryKey(id);

        if (exExamPapper == null) {
            return new ExExamPapper();
        }
        //获取试题信息
        ExQuestionsExample questionExample = new ExQuestionsExample();
        ExQuestionsExample.Criteria questionCriteria = questionExample.createCriteria();
        questionCriteria.andExamIdEqualTo(exExamPapper.getId());
        questionExample.setOrderByClause(" seq asc ");
        List<ExQuestions> questionList = questionMapper.selectByExample(questionExample);
        if(questionList!=null && questionList.size()>0){
            ExQuestionOptionExample optionExample ;
            ExQuestionOptionExample.Criteria optionCriteria;
            for(ExQuestions question : questionList){
                optionExample = new ExQuestionOptionExample();
                optionCriteria = optionExample.createCriteria();
                optionCriteria.andQuestionIdEqualTo(question.getId());
                optionExample.setOrderByClause(" seq asc ");
                List<ExQuestionOption> optionList = questionOptionMapper.selectByExample(optionExample);
                question.setOptionList(optionList);
            }
        }
        exExamPapper.setQuestionList(questionList);
        return exExamPapper;
    }
    private void delQuestionByExamId(Long examId){
        ExQuestionsExample questionExample = new ExQuestionsExample();
        ExQuestionsExample.Criteria questionCriteria = questionExample.createCriteria();
        questionCriteria.andExamIdEqualTo(examId);
        List<ExQuestions> questionList = questionMapper.selectByExample(questionExample);
        if(questionList!=null && questionList.size()>0){
            ExQuestionOptionExample optionExample ;
            ExQuestionOptionExample.Criteria optionCriteria;
            for(ExQuestions question : questionList){
                optionExample = new ExQuestionOptionExample();
                optionCriteria = optionExample.createCriteria();
                optionCriteria.andQuestionIdEqualTo(question.getId());
                questionOptionMapper.deleteByExample(optionExample);
                questionMapper.deleteByPrimaryKey(question.getId());
            }
        }
    }
    public void save(ExExamPapper examPapper) throws Exception {
        boolean isUpdate = false;
        Date curDate = new Date();
        if (examPapper.getId() != null && examPapper.getId() > 0) {
            examPapper.setUpdateTime(curDate);
            examPapper.setStatus(0);
            examPapperMapper.updateByPrimaryKey(examPapper);
            isUpdate = true;
        } else {
            examPapper.setCreateTime(curDate);
            examPapper.setUpdateTime(curDate);
            examPapperMapper.insert(examPapper);
        }
        //删除原有考题
        if (isUpdate) {
            this.delQuestionByExamId(examPapper.getId());
        }
        if (examPapper.getQuestionList() != null && examPapper.getQuestionList().size() > 0) {
            ExQuestions question = null;
            ExQuestionOption questionOption = null;
            for (int i = 0; i < examPapper.getQuestionList().size(); i++) {
                question = examPapper.getQuestionList().get(i);
                question.setExamId(examPapper.getId());
                if(question.getSeq()!=null) {
                    question.setSeq(question.getSeq());
                }else {
                    question.setSeq(i+1);
                }
                question.setQuestionType(1);
                questionMapper.insert(question);
                if (question.getOptionList() != null && question.getOptionList().size() > 0) {
                    for (int j = 0; j < question.getOptionList().size(); j++) {
                        questionOption = question.getOptionList().get(j);
                        questionOption.setQuestionId(question.getId());
                        questionOption.setSeq(j + 1);
                        questionOptionMapper.insert(questionOption);
                    }
                }
            }
        }
        TiTraining tiTraining  = new TiTraining();
        tiTraining.setId(examPapper.getTrainingId());
        tiTraining.setExamId(examPapper.getId());
        trainingService.updateExamById(tiTraining);
    }

    public List<Map<String,Object>> selectExamResultList(Map<String,Object> paramMap,Integer pageSize, Integer pageNum)  throws Exception{
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String,Object>> mapList =  exExamResultMapper.selectExamResultList(paramMap);
        return mapList;
    }
    public  List<ExExamResultView> exportExamResultList(Map<String,Object> paramMap, Integer pageSize, Integer pageNum)  throws Exception{
        PageHelper.startPage(pageNum, pageSize);
        List<ExExamResultView> list =  exExamResultMapper.exportExamResultList(paramMap);
        return list;
    }
}